<template>
	<view class="gwxz">
		<u-navbar back-text="返回"></u-navbar>

		<view class="content safe-area-inset-bottom">
			<view style="margin:10px 20px">
				<u-checkbox-group :wrap="true" size="50" max="1" v-if="id==1">
					<u-checkbox active-color="#4a9add" @change="checkboxChange" v-model="item.checked"
						v-for="(item, index) in list" :key="index" :name="item.postId">{{item.postName}}
					</u-checkbox>
				</u-checkbox-group>
			</view>


			<view style="margin:10px 20px">
				<u-checkbox-group :wrap="true" size="50" max="1" v-if="id==2">
					<u-checkbox active-color="#4a9add" @change="checkboxChange" v-model="item.checked"
						v-for="(item, index) in list" :key="index" :name="item.vehicleId">{{item.vehicleName}}
					</u-checkbox>
				</u-checkbox-group>
			</view>
		</view>
		<view class="footer safe-area-inset-bottom">
			<button style="background-color: #4a9add ;width: 50%;color: #fff;" @click="submit">确定</button>
		</view>

	</view>
</template>

<script>
	import {
		VehicleList,
		UserBorrow,
		PostList,
		UserPostGo,
		UserPostOff
	} from "@/pages/api/api"
	export default {
		data() {
			return {
				list: [],
				id: 0,
				items: null,


			};
		},
		onLoad(option) {
			this.id = option.id;
			if (option.id == 1) {
				PostList({
					status: 0
				}).then(res => {
					if (res.code == 200) {
						res.data.map(val => {
							val.checked = false;
						})
						this.list = res.data
					}
					console.log(res)
				})


			} else {

				VehicleList({
					pageNum: 1,
					useState: 0
				}).then(res => {

					if (res.code == 200) {
						res.rows.map(val => {
							val.checked = false;
						})

						this.list = res.rows
					}
					// console.log(123, res)
				})
			}

		},
		methods: {
			// 选中某个复选框时，由checkbox时触发
			checkboxChange(e) {
				if (this.id == 1) {
					let data = this.list.filter(val => {
						return val.postId == e.name;
					})
					this.items = data[0];

				} else {
					let data = this.list.filter(val => {
						return val.vehicleId == e.name;
					})
					this.items = data[0];
				}
			},
			submit() {
				if (this.id == 1) {
					UserPostOff(
						// {
						// 	userId:uni.getStorageSync('userId'),
						// }
					).then(res => { //这是下岗
						if (res.code == 200) {
							UserPostGo({ //这是上岗
								postId: this.items.postId,
								postName: this.items.postName
							}).then(res => {
								if (res.code == 200) {
									uni.showToast({
										title: '上岗成功',
										duration: 2000
									});
									setTimeout(() => {
										uni.navigateBack()
									}, 1500)
								}
							})

						}
					})






				} else {
					UserBorrow({
						vehicleId: this.items.vehicleId
					}).then(res => {

						if (res.code == 200) {
							uni.showToast({
								title: '借车成功',
								duration: 2000
							});
							setTimeout(() => {
								uni.navigateBack()
							}, 1500)

						}
					})
				}
			},

		}
	}
</script>

<style lang="scss" scoped>
	.gwxz /deep/ .u-checkbox__icon-wrap--square {
		float: right;
		right: 9%;
		position: absolute;
	}

	.gwxz /deep/ .u-checkbox__label {
		float: left;
		margin-left: 20px;
	}

	.gwxz /deep/.u-checkbox {
		background: #fff;
		margin: 5px 0;
		line-height: 2;
	}

	.footer {
		width: 100%;
		padding: 20rpx 0;
		position: fixed;
		bottom: 0;
		background: #fff;
		justify-content: center;
		display: flex;
		align-items: center;
	}

	.content {
		width: 100%;
		border-radius: 5px;
		height: calc(90% - 120rpx);
		;
		overflow: scroll;
		position: fixed;
		left: 0;
		right: 0;

	}
</style>
